package logic

import (
	"context"
	"fmt"
	"shop_srv/model/es"
	"shop_srv/model/mysql"
	"shop_srv/model/redis"

	"shop_srv/goods"
	"shop_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *goods.CreateGoodsRequest) (*goods.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  in.GoodsPrice,
		GoodsStock:  in.GoodsStock,
		GoodsCover:  in.GoodsCover,
		GoodsStatus: int8(in.GoodsStatus),
	}

	err := g.Create()
	if err != nil {
		fmt.Println("添加失败")
		return nil, err
	}
	//同步库存 to redis
	redis.SyncGoodsStock(int(g.ID), int(g.GoodsStock))
	//同步到es当中
	es.SyncGoods(&g)
	return &goods.CreateGoodsResponse{GoodsId: int64(g.ID)}, nil
}
